package com.chen.shopping_manager_api.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.shopping_common.pojo.Role;
import com.chen.shopping_common.result.BaseResult;
import com.chen.shopping_common.service.RoleService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.ibatis.annotations.Delete;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {

    @DubboReference
    private RoleService roleService;

    @PostMapping("/add")
    public BaseResult addRole(@RequestBody Role role){
        roleService.addRole(role);
        return BaseResult.ok();
    }

    @PutMapping("/update")
    public BaseResult updateRole(@RequestBody Role role){
        roleService.updateRole(role);
        return BaseResult.ok();
    }

    @DeleteMapping("/delete")
    public BaseResult deleteRole(Long rid){
        roleService.deleteRole(rid);
        return BaseResult.ok();
    }

    @GetMapping("/findById")
    public BaseResult findRoleById(Long rid){
        Role roleById = roleService.findRoleById(rid);
        return BaseResult.ok(roleById);
    }

    @GetMapping("/search")
    @PreAuthorize("hasAnyAuthority('/role/search')")
    public BaseResult findRoleByPage(int page,int size){
        Page<Role> roleByPage = roleService.findRoleByPage(page, size);
        return BaseResult.ok(roleByPage);
    }

    @GetMapping("/findAll")
    public BaseResult findRoleAll(){
        List<Role> allRole = roleService.findAllRole();
        return BaseResult.ok(allRole);
    }

    @PutMapping("/updatePermissionToRole")
    public BaseResult updatePermissionToRole(Long rid,Long[] pids){
        roleService.updateRolePermission(rid,pids);
        return BaseResult.ok();
    }
}
